package marksys.free.com.marksys.utils;

import java.util.ArrayList;
import java.util.List;

public class CollectionUtils {
    /**
     * 去除重复元素
     *
     * @param list 要去除重复的集合
     * @return 删除的元素集合
     */
    public static <T> List<T> removeRepeat(List<T> list) {
        return removeRepeat(list, false);
    }

    /**
     * 去除重复元素
     *
     * @param list   要去除重复的集合
     * @param equals 是否用equals去判断
     * @return 删除的元素集合
     */
    public static <T> List<T> removeRepeat(List<T> list, boolean equals) {
        List<T> removeList = new ArrayList<>(list.size() > 8 ? 8 : list.size() / 2);
        for (int i = 0; i < list.size() - 1; i++) {
            T a = list.get(i);
            for (int j = list.size() - 1; j > i; j--) {
                T b = list.get(j);
                if (a == b || (equals && a.equals(b))) {
                    list.remove(j);
                    removeList.add(b);
                }
            }
        }
        return removeList;
    }
}

